<?php

namespace App\Http\Service;

use App\Http\Util\Result;
use App\Model\Appoint;
use Illuminate\Support\Facades\DB;

trait ServiceAppoint{

    /**
     * czs
     * @return mixed
     * 社群列表
     */
    public static function getList($keyWord,$limit){
        $model = DB::table('appoints as a');
        if (!empty($keyWord)) {
            $model->where("b.name", "like", '%' . $keyWord . '%');
        }
        $list = $model
            ->leftJoin('sceneries as b','b.id','=','a.scenery_id')
            ->leftJoin('cards as c','c.id','=','a.card_id')
            ->select('a.*','b.name as bname','c.card_num')
            ->paginate($limit);
        return Result::success()->setData($list);
    }

    /**
     * @param $input
     * @return Result
     * 新增预约
     * czs
     */
    public static function createAppoint($input) {
        $input['appoint_time'] = strtotime($input['appoint_time']);
        $time = DB::table('sceneries as a')
            ->leftJoin('function_details as b','b.id','=','a.detail_id')
            ->where('a.id',$input['scenery_id'])
            ->where('b.type',5)
            ->select('b.start_time','b.end_time')
            ->first();
        $startTime = $time->start_time-7*24*3600;
        $endTime = $time->end_time-7*24*3600;
        if(!(time()>=$startTime&&time()<=$endTime)) {
            return Result::failure()->setMsg('请在景区开放时间内前七天预约！');
        }
        if(!($input['appoint_time']>=$time->start_time&&$input['appoint_time']<=$time->end_time)) {
            return Result::failure()->setMsg('预约时间不在景区开放时间内！');
        }
        $sel = DB::table('cards')->where('card_num',$input['card_num']);
        if ($sel->count()) {
            $input['card_id'] = $sel->pluck('id');
        } else {
            return Result::failure()->setMsg('卡号信息错误！');
        }
        unset($input['card_num']);
        $res = DB::table('appoints')->insert($input);
        if ($res) {
            return Result::success();
        } else {
            return Result::failure()->setMsg('创建失败！');
        }
    }

    /**
     * 取消预约
     * czs
     */
    public static function cancel($id) {
        $sel = DB::table('appoints')->where('id',$id)->first();
        $recordSel = DB::table('verify_records')
            ->where('scenery_id',$sel->scenery_id)
            ->where('card_id',$sel->card_id);
        if ($recordSel->count()) {
            return Result::failure()->setMsg('此卡已经超过使用次数！');
        }
        $res = DB::table('appoints')->where('id',$id)->delete();
        if ($res) {
            return Result::success();
        } else {
            return Result::failure()->setMsg('取消失败！');
        }

    }

    /**
     * czs
     *删除记录
     */
    public static function deleteLine($id) {
        $res = Community::where("id", $id)->delete();
        return Result::success()->setData($res);
    }

    /**
     *获取单个id记录
     * czs
     */
    public static function getOne($id) {
        $res = Community::where("id", $id)->first();
        return Result::success()->setData($res);
    }

    /**
     *更新记录
     * czs
     */
    public static function updateData($input, $id) {
        $res = DB::table('communities')->where("id", $id)->update($input);
        return Result::success()->setData($res);
    }

    /**
     * 禁用、启用
     * czs
     */
    public static function able($id,$able) {
        DB::table('communities')->where("id", $id)->update(['status' => $able]);
        if ($able==2) {
            return Result::success()->setMsg("在售")->setData(2);
        } else {
            return Result::success()->setMsg("禁用")->setData(1);
        }
    }

    /**
     * web调取社群
     * czs
     */
    public static function ajaxCommunity($rid) {
        $arr = [
            0=>39,1=>34,2=>37,3=>36,4=>31,
            5=>33,6=>32,7=>28,8=>12,9=>26,
            10=>23,11=>22,12=>29,13=>40,14=>11,
            15=>7,16=>21,17=>27,18=>25,19=>35,
            20=>15,21=>14,22=>2,23=>13,24=>24,
            25=>17,26=>18,27=>19,28=>16,29=>37,
            30=>30,31=>20,
        ];
        $rid = $arr[$rid];
        $res = DB::table('communities')->where('region_id',$rid)->get();
        return Result::success()->setData($res);
    }

    /**
     * 预约景区下拉
     * czs
     */
    public static function sceneryPull() {
        $list = DB::table('sceneries as a')
            ->leftJoin('function_details as b','b.id','=','a.detail_id')
            ->where('b.type',5)
            ->select('a.id','a.name')
            ->get();
        return Result::success()->setData($list);
    }
}


